package com.shlh.saas.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shlh.saas.entity.EmailSendRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 邮件发送记录Mapper接口
 */
@Mapper
public interface EmailSendRecordMapper extends BaseMapper<EmailSendRecord> {
    
    /**
     * 根据用户手机号分页查询邮件发送记录
     * @param page 分页对象
     * @param phone 用户手机号
     * @return 分页结果
     */
    @Select("SELECT * FROM email_send_records WHERE phone = #{phone} AND is_deleted = 0 ORDER BY send_time DESC")
    Page<EmailSendRecord> selectPageByPhone(Page<EmailSendRecord> page, @Param("phone") String phone);
    
    /**
     * 统计用户的邮件发送总数
     * @param phone 用户手机号
     * @return 发送总数
     */
    @Select("SELECT COUNT(*) FROM email_send_records WHERE phone = #{phone} AND is_deleted = 0")
    int countByPhone(@Param("phone") String phone);
    
    /**
     * 统计用户的邮件送达总数
     * @param phone 用户手机号
     * @return 送达总数
     */
    @Select("SELECT SUM(delivery_count) FROM email_send_records WHERE phone = #{phone} AND is_deleted = 0")
    int sumDeliveryCountByPhone(@Param("phone") String phone);
    
    /**
     * 统计用户的邮件阅读总数
     * @param phone 用户手机号
     * @return 阅读总数
     */
    @Select("SELECT SUM(read_count) FROM email_send_records WHERE phone = #{phone} AND is_deleted = 0")
    int sumReadCountByPhone(@Param("phone") String phone);

    @Update("UPDATE email_send_records set status = 2 WHERE id = #{sendEmailId}")
    int failEmailSend(@Param("sendEmailId")Long sendEmailId);

    @Update("UPDATE email_send_records set status = 1, delivery_count = #{success} WHERE id = #{sendEmailId}")
    int successEmailSend(@Param("sendEmailId")Long sendEmailId,@Param("success") int success);
}